<?php

namespace App\Extend;

class Upload
{

    public static function mkdirs($dir, $mode = 0777)
    {
        if (!is_dir($dir)) {
            self::mkdirs(dirname($dir), $mode);
            if (mkdir($dir, $mode)) {
                chmod($dir, $mode);
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    public static function fileExt($fileName)
    {
        $tmp = explode(".", $fileName);
        $fileExt = $tmp[count($tmp) - 1];
        return strtolower($fileExt);
    }

    public static function changeName($fileName)
    {
        //$name = date('His', time()) . '_' . rand(1000,9999);
        $name = md5(uniqid(rand()));
        $ext = self::fileExt($fileName);
        return $name . '.' . $ext;
    }

    public static function upload($basedir = '', $filename = '', $multi = false)
    {
        $upload = $_FILES[$filename];
        if (!$upload) {
            return [];
        }
        $now = date('Ymd');
        $dir = $basedir . $now . '/';
        if (self::mkdirs($dir) === false) {
            return [];
        }
        if ($multi) {
            $files = $upload['name'];
            $newfiles = [];
            foreach ($files as $key => $file) {
                $newfile = self::changeName($file);
                if (@move_uploaded_file($upload['tmp_name'][$key], $dir . $newfile)) {
                    $newfiles[$key] = [
                        'name' => $upload['name'][$key],
                        'size' => $upload['size'][$key],
                        'type' => $upload['type'][$key],
                        'ext' => self::fileExt($upload['name'][$key]),
                        'save' => strstr($dir . $newfile, '/upload'),
                    ];
                } elseif (@copy($upload['tmp_name'][$key], $dir . $newfile)) {
                    $newfiles[$key] = [
                        'name' => $upload['name'][$key],
                        'size' => $upload['size'][$key],
                        'type' => $upload['type'][$key],
                        'ext' => self::fileExt($upload['name'][$key]),
                        'save' => strstr($dir . $newfile, '/upload'),
                    ];
                } else {
                    $newfiles[$key] = [];
                }
            }
            return $newfiles;
        } else {
            $newfile = self::changeName($upload['name']);
            if (move_uploaded_file($upload['tmp_name'], $dir . $newfile)) {
                return [
                    'name' => $upload['name'],
                    'size' => $upload['size'],
                    'type' => $upload['type'],
                    'ext' => self::fileExt($upload['name']),
                    'save' => strstr($dir . $newfile, '/upload'),
                ];
            } elseif (@copy($upload['tmp_name'], $dir . $newfile)) {
                return [
                    'name' => $upload['name'],
                    'size' => $upload['size'],
                    'type' => $upload['type'],
                    'ext' => self::fileExt($upload['name']),
                    'save' => strstr($dir . $newfile, '/upload'),
                ];
            } else {
                return [];
            }
        }
    }

}
